package main

import (
	"fmt"
	"git.oschina.net/karla/tuik"
	"math/rand"
	"time"
)

var dotlist = make([]string, 0)

func main() {
	tuik.Init()
	defer tuik.Close()
	var msg []string
	msg = append(msg, "Start")
	TuikRedraw(msg, "msg")
	go redata()
loop:
	for {
		switch key := tuik.Input(); key {
		case "CtrlC":
			break loop
		default:
			msg = append(msg, "按下["+key+"]")
			TuikRedraw(msg, "msg")
		}
	}
}

var testdata []string

func redata() {
	for {
		dot := " "
		time.Sleep(1 * time.Second)
		strdata := rand.Intn(8)
		for i := 0; i < strdata; i++ {
			dot = dot + " "
		}
		for i := 0; i < (8 - strdata); i++ {
			dot = dot + "▍"
		}
		testdata = append(testdata, dot)
		TuikRedraw(testdata, "redata")
	}

}

var showmsg, showredata []string

func TuikRedraw(getmsg []string, msgkind string) {
	switch msgkind {
	case "msg":
		showmsg = getmsg
	case "redata":
		showredata = getmsg
	}
	windowWidth, windowHeight := tuik.Size()
	tuik.Label(tuik.Kind{
		Fg:      tuik.ColorCyan,
		Content: "窗口高:" + fmt.Sprint(windowHeight) + " 窗口宽:" + fmt.Sprint(windowWidth),
	})

	tuik.Label(tuik.Kind{
		Y:       1,
		Fg:      tuik.ColorGreen,
		Content: "Test Label[测试Label]",
	})

	tuik.Slips(tuik.Kind{
		X:       1,
		Y:       13,
		Fg:      tuik.ColorGreen,
		Content: "垂直内容测试",
	})

	a := tuik.Kind{
		X:      30,
		Y:      2,
		Width:  10,
		Height: 20,
		Border: []int{1, 1, 1, 1},
	}
	a.Block(tuik.Kind{
		X:          3,
		Y:          13,
		Width:      28,
		Height:     10,
		TitleColor: tuik.ColorYellow,
		Title:      "msg Test!",
		List:       showredata,
		ListWay:    "left",
	})
	a.Block(tuik.Kind{
		X:          33,
		Width:      30,
		Height:     10,
		Border:     []int{0, 0, 0, 0},
		Title:      "不加边框",
		TitleColor: tuik.ColorYellow,
		List:       showmsg,
		ListWay:    "right",
		Bg:         tuik.ColorBlue,
	})
	a.Block(tuik.Kind{
		X:          1,
		Width:      30,
		Height:     10,
		TitleColor: tuik.ColorYellow,
		Title:      "列表内容内容",
		List:       []string{"中文列表", "有一条", "中文列表X", "有loane条", "中文列表", "6有一条", "7中文列表", "8有一条", "9中文列表", "10有一条", "11中文列表", "12有一条", "13再一条"},
	})
	//MSG
	a.Block(tuik.Kind{
		X:          33,
		Y:          13,
		Width:      30,
		Height:     10,
		TitleColor: tuik.ColorYellow,
		Title:      "按键测试!",
		List:       showmsg,
		ListWay:    "top",
	})

	tuik.Flush()
}
